#!/bin/sh
# Pre-Electron argv wrapper for Super Productivity on Linux.
#
# Forces --ozone-platform=x11 when launched inside *our* Snap sandbox on a
# Wayland session. This is load-bearing: the programmatic
# app.commandLine.appendSwitch('ozone-platform','x11') in electron/start-app.ts
# is not enough in practice — field reports on issue #7270 show Chromium's
# Ozone init dlopens libEGL/libgbm on the core22 Mesa path before
# appendSwitch is honored, which segfaults under host-vs-snap Mesa ABI
# drift. Putting the flag into argv before Electron's argv parser runs
# bypasses that.
#
# Same mechanism used by Signal Desktop and Mattermost Desktop snaps
# (snapcrafters/signal-desktop, snapcrafters/mattermost-desktop).
#
# All Linux launches pass through this wrapper, but only our Snap on Wayland
# receives the extra ozone flag; every other launch is a plain exec. The
# SNAP_NAME gate protects .deb/.rpm installs invoked via xdg-open from *another*
# snap (where $SNAP leaks into the child env).
#
# This wrapper deliberately does NOT pass --class: Electron ignores that switch.
# See tools/verify-linux-wm-class.test.js and #9674.
#
# See docs/research/snap-wayland-gpu-fix-research.md.

# Derive the real ELF path. Inside our snap confinement, $SNAP is the
# revision mount root and is more reliable than $0 resolution through
# snapd's wrapper chain. Elsewhere, resolve $0 through symlinks — this
# handles /usr/bin/superproductivity symlinks from .deb/.rpm installs.
if [ -n "$SNAP" ] && [ "$SNAP_NAME" = "superproductivity" ]; then
  IS_OUR_SNAP=1
  BIN_DIR="$SNAP"
else
  IS_OUR_SNAP=
  SELF=$(readlink -f "$0" 2>/dev/null || echo "$0")
  BIN_DIR=$(dirname "$SELF")
fi
BIN="$BIN_DIR/superproductivity-bin"

# If the user already supplied --ozone-platform on argv, don't override. Stop
# scanning at -- so positional args aren't misread as flags.
HAS_OZONE_PLATFORM=
for arg in "$@"; do
  case "$arg" in
    --) break ;;
    --ozone-platform=* | --ozone-platform) HAS_OZONE_PLATFORM=1 ;;
  esac
done

if [ -n "$IS_OUR_SNAP" ] && [ -z "$HAS_OZONE_PLATFORM" ] && { [ "$XDG_SESSION_TYPE" = "wayland" ] || [ -n "$WAYLAND_DISPLAY" ]; }; then
  exec "$BIN" --ozone-platform=x11 "$@"
fi

exec "$BIN" "$@"
